<template>
  <div class="app-container">
    <div class="box">
      <el-row class="head">
        <el-col class="title">名称</el-col>
        <el-col class="title">介绍名称</el-col>
        <el-col class="title">标签</el-col>
        <el-col class="title">优惠劵类型</el-col>
        <el-col class="title">最低消费</el-col>
        <el-col class="title">优惠面值</el-col>
      </el-row>
      <el-row class="head">
        <el-col class="content">{{ list.name }}</el-col>
        <el-col class="content">{{ list.desc }}</el-col>
        <el-col class="content">{{ list.tag }}</el-col>
        <el-col class="content">{{ list.type | getType }}</el-col>
        <el-col class="content">满{{ list.min }}元可用</el-col>
        <el-col class="content">减免{{ list.discount }}元</el-col>
      </el-row>
      <el-row class="head">
        <el-col class="title">每人限额</el-col>
        <el-col class="title">当前状态</el-col>
        <el-col class="title">商品范围</el-col>
        <el-col class="title">有效期</el-col>
        <el-col class="title">优惠兑换码</el-col>
        <el-col class="title">发行数量</el-col>
      </el-row>
      <el-row class="head">
        <el-col class="content">{{ list.limit }}</el-col>
        <el-col class="content">{{ list.status | getStatus }}</el-col>
        <el-col class="content">{{ list.goodsType | getGoodsType }}</el-col>
        <el-col class="content">{{ getTimeScope() }}</el-col>
        <el-col class="content">{{ list.code }}</el-col>
        <el-col class="content">{{ list.total }}</el-col>
      </el-row>
    </div>
    <!-- 查询操作 -->
    <div class="main_tjy">
      <div class="Tb">
        <el-input
          v-model="searchIpt"
          placeholder="请输入用户ID"
          clearable
          class="input1"
        />
        <el-select
          v-model="searchIpt1"
          clearable
          placeholder="请选择使用状态"
          class="input2"
        >
          <el-option
            v-for="(item, index) in arr"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch"
          >查找</el-button
        >
      </div>
      <div class="bottom">
        <!-- 查询结果 -->
        <el-table
          v-loading="listLoading"
          :data="list1"
          element-loading-text="正在查询中。。。"
          border
          fit
          highlight-current-row
        >
          <el-table-column
            align="center"
            label="用户优惠券ID"
            prop="id"
            sortable
          />
          <el-table-column align="center" label="用户ID" prop="userId" />
          <el-table-column align="center" label="领取时间" prop="addTime" />
          <el-table-column align="center" label="使用状态" prop="status">
            <template slot-scope="scope">{{
              scope.row.status | getUseStatus
            }}</template>
          </el-table-column>
          <el-table-column align="center" label="订单ID" prop="orderId" />
          <el-table-column align="center" label="使用时间" prop="usedTime" />
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="listQuery.page" 
          v-model:limit="listQuery.limit"
          @pagination="getList"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { readCoupon, listCouponUser } from "@/api/tggl/coupon";
import Pagination from "@/components/Pagination";
const Type = [
  {
    label: "通用领券",
    value: 0,
  },
  {
    label: "注册赠券",
    value: 1,
  },
  {
    label: "兑换码",
    value: 2,
  },
];
export default {
  filters: {
    // 优惠券类型
    getType(type) {
      for (let i = 0; i < Type.length; i++) {
        if (type === Type[i].value) {
          return Type[i].label;
        }
      }
      return "";
    },
    // 当前状态
    getStatus(status) {
      if (status === 0) {
        return "正常";
      } else if (status === 1) {
        return "已过期";
      } else {
        return "已下架";
      }
    },
    // 商品范围
    getGoodsType(goods) {
      if (goods === 0) {
        return "全场通用";
      } else if (goods === 1) {
        return "指定分类";
      } else {
        return "指定商品";
      }
    },
    // 查询使用状态
    getUseStatus(status) {
      if (status === 0) {
        return "未使用";
      } else if (status === 1) {
        return "已使用";
      } else if (status === 3) {
        return "已过期";
      } else {
        return "已下架";
      }
    },
  },
  data() {
    return {
      list: {},
      searchIpt: "",
      searchIpt1: "",
      listLoading: true,
      total: 0,
      listQuery: {
        page: 1,
        limit: 20,
        couponId: 1,
        userId: undefined,
        status: undefined,
        sort: "add_time",
        order: "desc",
      },
      list1: [],
      arr: [
        {
          value: 0,
          label: "未使用",
        },
        {
          value: 1,
          label: "已使用",
        },
        {
          value: 2,
          label: "已过期",
        },
        {
          value: 3,
          label: "已下架",
        },
      ],
    };
  },
  components: { Pagination },
  mounted() {
    const couponId = this.$route.query.id;
    this.listQuery.couponId = couponId;
    this.handleList();
  },
  methods: {
    // 初始列表
    async handleList() {
      const res = await readCoupon(this.$route.query.id);
      this.list = res.data;
      this.getList();
    },
    getTimeScope() {
      if (this.list.timeType === 0) {
        return "领取" + this.list.days + "天有效";
      } else if (this.list.timeType === 1) {
        return "自" + this.list.startTime + "至" + this.list.endTime + "有效";
      } else {
        return "未知";
      }
    },
    // 查询列表
    async getList() {
      this.listLoading = true;
      const res = await listCouponUser(this.listQuery);
      console.log(res, "列表");
      this.list1 = res.data.list;
      this.total = res.data.total;
      this.listLoading = false;
    },
    // 查询
    handleSearch() {
      this.listQuery.page = 1; // 重置分页
      this.listQuery.userId = this.searchIpt; // 将用户输入的 userId 传递到查询参数
      this.listQuery.status = this.searchIpt1; // 将用户选择的 status 传递到查询参数
      this.getList();
    },
  },
};
</script>

<style>
.app-container {
  padding: 20px;
}

/* 输入框 */
.el-input__inner {
  font-size: 12px;
  height: 28px;
  line-height: 28px;
}

.el-form-item {
  margin-bottom: 5px;
}

.el-range-editor.el-input__inner {
  height: 28px;
  vertical-align: middle;
}
.input2 {
  width: 200px;
}
.input1 {
  width: 200px;
}

/* 按钮 */
.el-button {
  font-size: 12px;
  padding: 7px 15px;
}

/* 表格 */
.el-table {
  font-size: 12px;
  margin-top: 20px;
}

.el-table td,
.el-table th {
  padding: 6px 0;
}

.box {
  margin-top: 20px;
  border-left: 1px solid #dcdfe6;
  border-top: 1px solid #dcdfe6;
}

.box > .head {
  display: flex;
}

.box > .head > .title {
  border-right: 1px solid #dcdfe6;
  border-bottom: 1px solid #dcdfe6;
  padding: 10px;
  background: #f2f6fc;
  text-align: center;
  font-size: 14px;
  color: #303133;
}

.box > .head > .content {
  height: 60px;
  line-height: 40px;
  border-right: 1px solid #dcdfe6;
  border-bottom: 1px solid #dcdfe6;
  padding: 10px;
  font-size: 14px;
  color: #606266;
  text-align: center;
  overflow: hidden;
}

.main_tjy {
  margin-top: 20px;
}

.Tb {
  display: flex;
}

.Tb > .el-input--mini {
  width: 200px;
  margin-right: 10px;
}

.bottom {
  margin-top: 20px;
}
</style>